// pages/home//unitDetail/unitDetail.js
import {
  input
} from '../../../utils/tools'
import { http } from '../../../utils/apiutils'
import { getaatime } from '../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    popShow: false,
    showAdd: true,
    quantity: '',
    editQuantity: '',
    jobId: '',
    userId: '',
    createTime: '',
    userName: '',
    totalNum: 0,
    totalMoney: 0,
    jobInfo: {},
    dataList: [],
    currentEditId: 0,
    workDayTime:'',
    editable:true,
    showAppeal:false,
    currentRemark:'',
    amount:0,
    num:0


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      jobId: options.jId - 0,
      userId: options.uId - 0,
      workDayTime:options.cTime,
      createTime: options.cTime.slice(5),
      userName: options.name,
    })
    this.getJobDetail(this.data.jobId)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let currentDay=new Date().getTime()
    let workDay=new Date((this.data.workDayTime+' 23:59:00').replace(/-/g,'/')).getTime()
    if(currentDay>workDay){
      this.setData({
        editable:false
      })
    }

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDayWork({ userId: this.data.userId, createTime: this.data.createTime })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onPopClose() {
    this.setData({
      popShow: false,
    })
  },
  clickCancel() {
    this.setData({
      popShow: false,
      quantity: '',
      editQuantity: ''
    })
  },
  clickConfirm(e) {
    var that = this
    if (e.currentTarget.dataset.key == 1) {
      if (!that.data.quantity) {
        wx.showToast({
          title: '请输入正确数量',
          icon: 'none'
        })
        return
      }
      let params = {
        jobId: that.data.jobId,
        userId: that.data.userId,
        num: that.data.quantity,
      }
      // 提交
      that.addUnitLog(params)
    } else if (e.currentTarget.dataset.key == 2) {
      if (!that.data.editQuantity) {
        wx.showToast({
          title: '请输入正确数量',
          icon: 'none'
        })
        return
      }
      let params = {
        id: that.data.currentEditId,
        jobId: that.data.jobId,
        userId: that.data.userId,
        num: that.data.editQuantity,
        appeal:0
      }
      that.editUnitLog(params)
    }

  },
  input(event) {
    input.bind(this)(event)
  },
  // 点击添加
  addLog() {
    this.setData({
      popShow: true,
      showAdd: true
    })
  },
  // 当天工作列表
  getDayWork(params) {
    http.getDayWork(params, res => {
      if (res.status == 200) {
        if (res.obj && res.obj.list.length > 0) {
          let m = 0
          let n = 0
          res.obj.list.map(item => {
            m = m + item.amount
            n = n + item.num
            item.time1 = item.createTime?getaatime(item.createTime):''
            item.time2 = item.createTime?item.createTime.split(' ')[1].slice(0, 5):''
            item.time3 = item.updateTime?item.updateTime.split(' ')[1].slice(0, 5):''
          })
          wx.nextTick(_ => {
            this.setData({
              totalNum: n,
              totalMoney: m,
              amount:res.obj.amount,
              num:res.obj.num,
              dataList: res.obj.list,
            })
          })
        }
      }
    }, err => { })
  },
  // 获取岗位详情
  getJobDetail(id) {
    var that = this
    http.getJobData(id, res => {
      // console.log('成功', res)
      if (res.code == 0) {
        that.setData({
          jobInfo: res.data
        })
      } else {
        wx.showToast({
          title: res.msg || res.message,
          icon: 'none'
        })
      }
    }, err => { })
  },
  addUnitLog(params) {
    http.addUnitLog(params, res => {
      if (res.status == 200) {
        this.setData({
          popShow: false,
          quantity: ''
        })
        this.getDayWork({ userId: this.data.userId, createTime: this.data.createTime })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }, err => { })
  },
  // 点击编辑
  onClickEdit(e) {
    this.setData({
      showAdd: false,
      editQuantity: e.currentTarget.dataset.info.num,
      currentEditId: e.currentTarget.dataset.info.id,
      popShow: true,
    })
  },
  editUnitLog(params) {
    http.editUnitLog(params, res => {
      if (res.status == 200) {
        this.setData({
          popShow: false,
          editQuantity: ''
        })
        this.getDayWork({ userId: this.data.userId, createTime: this.data.createTime })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }, err => { })
  },
  showAppealPop(e){
    this.setData({
      currentRemark:e.currentTarget.dataset.info.remark,
      showAppeal:true
    })
  },
  onAppealClose(){
    this.setData({
      showAppeal:false
    })
  }


})